// view/index/public/public.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '',
    huan: [],
    currtab: 0,
    addpub: '',
    sblist: [],
    gyslist: [],
    id: '',
    gongwu: false,
    page: 1,
    keywords: '',
    cid:'',
    shbeilist:[],
    keywordsone:"",
    start: '开始时间',
    endtime: '结束时间',
    begintime: '',
    endtimes: '',
  },
  bindkaishi(e) {
    this.setData({
      start: e.detail.value,
      begintime: e.detail.value,
    })
    this.xdxdjk()

  },
  bindjieshu(e) {
    if (e.detail.value < this.data.begintime) {
      wx.showToast({
        title: '结束时间不能小于开始时间',
        icon: 'none'
      })
    } else {
      this.setData({
        endtime: e.detail.value,
        endtimes: e.detail.value,
      })
      this.xdxdjk()
    }
  },
  chonzghia(){
    this.setData({
      keywordsone: '',
      endtimes: '',
      start: '开始时间',
    endtime: '结束时间',
      begintime:'',
    })
    this.xdxdjk()

  },
  // 查看更多
  lookmore() {
    if (this.data.currtab == 1) {
      shuzong = this.data.sblist
    } else if (this.data.currtab == 2) {
      shuzong = this.data.gyslist
    }
    this.setData({
      page: (this.data.page) + 1
    })
    if (this.data.currtab == 1) {
      this.xdxdjk();
    } else if (this.data.currtab == 2) {
      this.getgys();
    }
  },
  // 接口
  xdxdjk() {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    var u = '';
    if (that.data.id == 5) {
      u = 'shop/shopSelectCleanlog'
    }
    if (that.data.id == 6) {
      u = 'shop/shopSelectGreaselog'
    }
    if (that.data.id == 7) {
      u = 'Shop/shopSelectFoodquality'
    }
    if (that.data.id == 14) {
      u = 'shop/shopSelectPestlog'
    }
    if (that.data.id == 8) {
      u = 'shop/shopSelectitiveLog'
    }
    this.myRequest(yuming + u, {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      keywords:this.data.keywordsone,
      start_time:this.data.begintime,
      end_time:this.data.endtimes
    }, "POST").then(res => {
      wx.hideLoading();
      shu = res.data.data
      if (that.data.page != 1) {
        if (shu.length != 10) {
          wx.showToast({
            title: '没有更多数据了~',
            icon: "none"
          })
          that.setData({
            gongwu: true
          })
        }
      }
      that.setData({
        sblist: shuzong.concat(shu)
      })
    })
  },

  // 设别删除
  shanSB(e){
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.myRequest(yuming +'Shopthat/clean_device_del',{
            shopId: wx.getStorageSync('shopId'),
            id: id
      
          },'POST').then(res =>{
            if(res.data.code == 200){
              that.shbeilist()
            }
          })
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 删除
  shan(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    console.log(e);
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.dele(id)
          // that.shbeilist()
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 删除接口
  dele(id) {
    let that = this
    var u = '';
    if (that.data.id == 5) {
      u = 'shop/shopDelCleanlog'
    }
    if (that.data.id == 6) {
      u = 'shop/shopDelGreaselog'
    }
    if (that.data.id == 7) {
      u = 'Shop/shopDelFoodquality'
    }
    if (that.data.id == 14) {
      u = 'shop/shopDelPestlog'
    }
    if (that.data.id == 8) {
      u = 'shop/shopDelitiveLog'
    }
    this.myRequest(yuming + u, {
      shopId: wx.getStorageSync('shopId'),
      id: id
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.xdxdjk()
      }
    })
  },

  shbeilist(){
    // var that = this
    this.myRequest(yuming +'Shopthat/clean_device_list',{
      shopId: wx.getStorageSync('shopId'),
      page: this.data.page,
      keywords: this.data.keywords

    },'POST').then(res =>{
      console.log( res.data.data);
      this.setData({
        shbeilist:res.data.data
      })
      // this.shbeilist = 
    })
  },
  // 知识产权 点击放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    var arr = []
    for (let i in list) {
      arr.push(list[i]); //属性
    }
    wx.previewImage({
      current: img,
      urls: arr
    })
  },
  // 知识产权 点击放大
  fangdas(e) {
    var img = e.currentTarget.dataset.img
    console.log(img);
    var arr = []
    
      arr.push(img); //属性
    
    wx.previewImage({
      current: img,
      urls: arr
    })
  },

  // tab切换
  qies(e) {
    shuzong = [];
    shu = [];
    var index = e.currentTarget.dataset.currtab
    this.setData({
      currtab: index,
      page: 1,
      gongwu: false,
      keywords:''
    })
    if (this.data.currtab == 0) {
      this.xdxdjk();
      this.shbeilist()

    } else if (this.data.currtab == 1) {
      // if(this.data.id == 5){
        this.shbeilist()

      // }
      this.getgys();  // 这里
    }
  },
  //获取供应商
  getgys() {
    let that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'shop/ShopSelectSupplier', {
      addid: that.data.id,
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      keywords: that.data.keywords
    }, "POST").then(res => {
      wx.hideLoading();
      shu = res.data.data
      if (that.data.page != 1) {
        if (shu.length != 10) {
          wx.showToast({
            title: '没有更多数据了~',
            icon: "none"
          })
          that.setData({
            gongwu: true
          })
        }
      }
      that.setData({
        gyslist: shuzong.concat(shu)
      })
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  // 添加
  jias() {
    wx.navigateTo({
      url: '/page_two/addpublics/addpublics?title=' + this.data.addpub + "&id=" + this.data.id + "&cid=" + this.data.cid,
    })
  },
  jiasxdxd(){
    wx.navigateTo({
      url: '/page_two/xdxd/xdxd',
    })
  },
  // 制度
  zhidu() {
    let that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'shop/ShopFindCartInfo', {
      shopId: wx.getStorageSync('shopId'),
      cid: that.data.cid
    }, "POST").then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        that.setData({
          xq: res.data.data,
          content: res.data.data.colExplain.replace(/(\<img|\<p)/gi, function ($0, $1) {
            return {
              "<img": '<img style="width:100%;height:auto;display:block;" ',
              "<p": '<p style="text-indent: 2em;" ',
              "<article": "<div",
              "</article": "</div",
              "<header": "<div",
              "</header": "</div"
            } [$1];
          })
        })
      }
    })
  },

  // 报备供应商方法
  // 供应商搜索
  keywords(e) {
    // console.log(e)
    var key = e.detail.value
    this.setData({
      keywords: key
    })
    this.getgys();
    this.shbeilist()
  },

  keywordsone(e) {
    console.log(e)
    var key = e.detail.value
    this.setData({
      keywordsone: key
    })


    this.xdxdjk()
    // this.getgys();
    // this.shbeilist()
  },


  // 添加供应商
  xinzeng(e) {
    let addid = e.currentTarget.dataset.addid
    let addtitle = ''
    console.log(addid);
    if(addid == 6){

      addtitle = '报备收购商'
    }else if(addid == 7){
      addtitle = '报备质检商'

    }else if(addid == 8 || addid == 14){
      addtitle = '报备供应商'

    }
    wx.navigateTo({
      url: '/page_one/addgys/addgys?addid=' + addid + '&addtitle=' + addtitle,
    })
  },
  // 删除供应商
  delete(e) {
    let id = e.currentTarget.dataset.id
    let that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '否',
      confirmText: '是',
      success(res) {
        if (res.confirm) {
          console.log("是")
          that.shangys(id)
        } else {
          console.log("否")
        }
      }
    })
  },
  shangys(id) {
    let that = this;
    this.myRequest(yuming + 'shop/ShopDelSupplier', {
      id,
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.getgys();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      id: options.cid,
      cid: options.cid
    })
    if (this.data.id == 5) {
    //   this.setData({
    //     huan: ['洗涤消毒制度', '洗涤消毒上报'],
    //     addpub: '洗涤消毒上报',
    //   })
      this.setData({
        huan: ['洗涤消毒上报','设备报备'],
        addpub: '洗涤消毒上报',
      })
    }
    if (this.data.id == 6) {
      this.setData({
        huan: [ '废弃油脂上报', '报备收购商'],
        addpub: '废弃油脂上报',
      })
    }
    if (this.data.id == 7) {
      this.setData({
        huan: [ '食材质检上报', '报备质检商'],
        addpub: '食材质检上报',
      })
    }
    if (this.data.id == 14) {
      this.setData({
        huan: ['灭虫管理上报', '报备供应商'],
        addpub: '灭虫管理上报',
      })
    }
    if (this.data.id == 8) {
      this.setData({
        huan: [ '添加剂上报', '报备供应商'],
        addpub: '添加剂管理',
      })
    }
    this.zhidu()   // 制度
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    shuzong = [];
    shu = [];
    this.setData({
      page: 1,
      gongwu: false
    })
    if (this.data.currtab == 0) {
        this.xdxdjk();
    } else if (this.data.currtab == 1) {
      this.getgys();
    this.shbeilist()

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})